package com.itstudio.myforumsystem.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itstudio.myforumsystem.common.Constants;
import com.itstudio.myforumsystem.entity.User;
import com.itstudio.myforumsystem.entity.dto.UserDTO;
import com.itstudio.myforumsystem.exception.ServiceExcepetion;
import com.itstudio.myforumsystem.mapper.UserMapper;
import com.itstudio.myforumsystem.utils.TokenUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService extends ServiceImpl<UserMapper,User>   {

    @Autowired
    private UserMapper userMapper;


    public UserDTO login(UserDTO userDTO) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("username",userDTO.getUsername());
        queryWrapper.eq("password",userDTO.getPassword());
        User one;
        try{
            one  =  getOne(queryWrapper);  //可能会存在脏数据的情况导致没法查询数据  但是目前来说的事满足需求

        }catch (Exception e){
            throw  new RuntimeException("查询数据失败");
        }
        if(one!=null){
            System.out.println("------------------------------------------------------------------------------"+one);
            BeanUtils.copyProperties(one,userDTO);//忽略大小写的复制给userDto
            System.out.println("-------------------------------"+userDTO);
            String token = TokenUtils.genToken(one.getId().toString(),one.getPassword());
            System.out.println(token);
            userDTO.setToken(token);
            return userDTO;
        }else{
            throw new RuntimeException("用户名或密码错误");
        }
    }

    public UserDTO adminLogin(UserDTO userDTO) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("username",userDTO.getUsername());
        queryWrapper.eq("password",userDTO.getPassword());
        queryWrapper.eq("role",userDTO.getRole());
        User one;
        try{
            one  =  getOne(queryWrapper);  //可能会存在脏数据的情况导致没法查询数据  但是目前来说的事满足需求

        }catch (Exception e){
            throw  new ServiceExcepetion(Constants.CODE_500,"系统错误");
        }
        if(one!=null){
            System.out.println("------------------------------------------------------------------------------"+one);
            BeanUtils.copyProperties(one,userDTO);//忽略大小写的复制给userDto
            System.out.println("-------------------------------"+userDTO);
            String token = TokenUtils.genToken(one.getId().toString(),one.getPassword());
            System.out.println(token);
            userDTO.setToken(token);
            return userDTO;
        }else{
            throw new ServiceExcepetion(Constants.CODE_600,"管理员身份不符或用户名密码不正确");
        }

    }

    public Integer register(String username) {
        Integer num=userMapper.findName(username);


        return num;
    }

    public User UserDataFindByUsername(String username) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("username",username);

        User one = getOne(queryWrapper);

        return  one ;

    }

    public User findUserByArticle_user(Integer articleId) {
        return userMapper.findUserByArticle(articleId);
    }

    public User allInforOfPersonal(Integer userId) {


        return userMapper.allInforOfPersonal(userId);
    }


    public List<User> fiveUser(){
        return  userMapper.limitFiveUser();
    }

    public Integer findReadNumber(Integer id) {
        return userMapper.findReadNumber(id);
    }

    public Integer findCollectionNumber(Integer id) {
        return  userMapper.findCollectionNumber(id);
    }

//    void save(User user);
//
//
//    List<User> list();
//
//    boolean removeById(Integer id);
//
//

//
//    User getById(Integer integer);




}
